package com.kongshidian.ui.fragment;

import java.lang.ref.WeakReference;
import java.util.List;

import com.kongshidian.App;
import com.kongshidian.R;
import com.kongshidian.bean.ServiceResult;
import com.kongshidian.http.WebClient;
import com.kongshidian.http.WebException;
import com.kongshidian.ui.LoginActivity;
import com.kongshidian.ui.Register2Activity;
import com.kongshidian.ui.RegisterActivity;
import com.kongshidian.util.ContextUtil;
import com.kongshidian.util.StringUtil;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;


/**
 * 添加空时店
 * @author ghg
 *
 */
@SuppressLint("HandlerLeak")
public class AddKSDFragment extends BaseFragment{
	
	
	private EditText mAddCardNameEdit ,mAddCardNoEdit;
	private Button mAddBut;
	private ServiceResult<String> ret;
	
	private Handler mHandler = new MyHandler(this);

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  
			Bundle savedInstanceState) { 
		return inflater.inflate(R.layout.fragement_addksd, container, false);  
	}  
	
	
	/**
	 * 创建以后做的事情
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mAddCardNameEdit = (EditText)	findViewById(R.id.add_cardname_edit) ;
		mAddCardNoEdit = (EditText)findViewById(R.id.add_cardno_edit);
		mAddBut = 	(Button)findViewById(R.id.add_but);
		
		setEditFocusChangeLister();
		
		mAddBut.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkCard();
			}
		});
	}
	
	
	private void checkCard() {

		new Thread(){
			public void run() {
				try {
					ret = WebClient.getInstance(getActivity()).checkCard(mAddCardNameEdit.getText().toString(),mAddCardNoEdit.getText().toString());
					mHandler.sendEmptyMessage(ret.getCode());
				} catch (WebException e) {
					mHandler.sendEmptyMessage(0); //通知卡名已存在
				}
			}
		}.start();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	private void setEditFocusChangeLister() {

		/*监听用户名EditText失去焦点的时候判断用户名是否符合规定 */
		mAddCardNameEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					if(TextUtils.isEmpty(mAddCardNameEdit.getText().toString())){
						mAddCardNameEdit.setError(getString(R.string.txt_info_card_name));
						return;
					}
				}
			}
		});

		mAddCardNoEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					if(TextUtils.isEmpty(mAddCardNoEdit.getText().toString())){
						mAddCardNoEdit.setError(getString(R.string.txt_info_card_no));
						return;
					}
					//验证是不是数字
					if( StringUtil.regxStr(mAddCardNoEdit.getText().toString(), "^[0-9]*$")){
						mAddCardNoEdit.setError(getString(R.string.txt_info_true_card_no));
						return;
					}
				}
			}
		});

	}
	
	
	
	
	private class MyHandler extends Handler{
		
		private WeakReference<Fragment> mActivity;
		
		private MyHandler(Fragment activity){
			mActivity = new WeakReference<Fragment>(activity);
		}
		
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					//mRegisterCardNameEdit.setError(getString(R.string.info_please_input_cardName_is_notexist));
					App.showShortToast(getString(R.string.request_failure));
					break;
				case 1:
					//注册成功
					//App.showLongToast(getString(R.string.card_success));
					mAddCardNameEdit.setText("");
					mAddCardNoEdit.setText("");
					AlertDialog.Builder builder = new AlertDialog.Builder(AddKSDFragment.this.getActivity());
					AlertDialog alertDialog =	builder.setMessage(ret.getData()).setCancelable(true).create();
					alertDialog.show();
					
					//finish();
					break;
				case 2:
					//注册失败
					App.showShortToast(getString(R.string.txt_info_true_card));
					break;
				case 3:
					LoginActivity.instance.finish();
					break;

				default:
					break;
				}
			}
		}
	}
	
	
}
